package org.yray.tts.media;


import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ByteUtils {

    public static byte[] uLongToByteWord(long j) {
        int i = (int) j;
        byte[] byteWord = new byte[4];
        byteWord[0] = (byte) ((i >>> 24) & 0x000000FF);
        byteWord[1] = (byte) ((i >> 16) & 0x000000FF);
        byteWord[2] = (byte) ((i >> 8) & 0x000000FF);
        byteWord[3] = (byte) (i & 0x00FF);
        return byteWord;
    }

    public static byte[] uIntToByteWord(int i) {
        byte[] byteWord = new byte[2];
        byteWord[0] = (byte) ((i >> 8) & 0x000000FF);
        byteWord[1] = (byte) (i & 0x00FF);
        return byteWord;
    }

    public static int uBytes2Int(byte b1,byte b2) {
        int i=0;
        i+=((b1&0xFF)<<8);
        i+=((b2&0xFF));
        return i;
    }

    public static int uBytes2Int(byte b1,byte b2,byte b3,byte b4) {
        int i=0;
        i+=((b1&0xFF)<<24);
        i+=((b2&0xFF)<<16);
        i+=((b3&0xFF)<<8);
        i+=((b4&0xFF));
        return i;
    }
    public static long uBytes2IntSip(byte[] bs) {
        long result=0;
        for(byte b:bs)
        {
            result = result<<4;
            result += (b & 0xf0) >> 4;
            result = result<<4;
            result+=(b & 0x0f) ;
        }
        return result;
    }
    public static int u8Bytes2Long(byte[] bs) {
        int i=0;
        i+=((bs[0]&0xFF)<<80);
        i+=((bs[1]&0xFF)<<64);
        i+=((bs[2]&0xFF)<<48);
        i+=((bs[3]&0xFF)<<36);
        i+=((bs[4]&0xFF)<<24);
        i+=((bs[5]&0xFF)<<16);
        i+=((bs[6]&0xFF)<<8);
        i+=((bs[7]&0xFF));
        return i;
    }

    public static long uBytes2Long(byte b1,byte b2,byte b3,byte b4) {
        return (long)((b1 & 0xffL) <<24 | (b2 & 0xffL) << 16 | (b3 & 0xffL) <<8 | (b4 & 0xffL));
    }

    public final static  byte[] uIntToFourByteWord(int value)  {
        byte[] result = new byte[4];
        if ((value > Math.pow(2,63)) || (value < 0)) {
            log.error("Integer value " + value + " is larger than 2^63");
        }
        result[0] = (byte)((value >>> 24) & 0xFF);
        result[1] = (byte)((value >>> 16) & 0xFF);
        result[2] = (byte)((value >>> 8) & 0xFF);
        result[3] = (byte)(value & 0xFF);
        return result;
    }

    public final static  byte[] uLongTo8ByteWord(long value)  {
        byte[] result = new byte[8];
        result[0] = (byte)((value >>> 80) & 0xFF);
        result[1] = (byte)((value >>> 64) & 0xFF);
        result[2] = (byte)((value >>> 48) & 0xFF);
        result[3] = (byte)((value >>> 32) & 0xFF);
        result[4] = (byte)((value >>> 24) & 0xFF);
        result[5] = (byte)((value >>> 16) & 0xFF);
        result[6] = (byte)((value >>> 8) & 0xFF);
        result[7] = (byte)(value & 0xFF);
        return result;
    }

    public static void main(String[] args) {
        /*byte a=(byte) 0x0FF;
        byte b=(byte)0x0F0;
        int c= a&b;
        int d = b >>> 1;
        System.out.println(c);
        System.out.println(d);*/
        /*byte[0] 记录 8—1位 ——不移动——> b[0] = (byte) int;
        byte[1] 记录 16—9位 ——右移8位——> b[1] = (byte) (int >> 8 & 0xff);
        byte[2] 记录 24—17位 ——右移16位——> b[2] = (byte) (int >> 16 & 0xff);
        byte[3] 记录 32—25位 ——右移24位——>b[3] = (byte) (int >> 24 & 0xff);

        byte[0] 记录 8—1位 ——不移动——> b[0] = (byte) int;
        byte[1] 记录 16—9位 ——右移8位——> b[1] = (byte) (int >> 8);
        byte[2] 记录 24—17位 ——右移16位——> b[2] = (byte) (int >> 16);
        byte[3] 记录 32—25位 ——右移24位——>b[3] = (byte) (int >> 24);

        */

        System.out.println(Integer.MAX_VALUE);
        //System.out.println(Arrays.toString(ByteUtils.uLongTo8ByteWord(BasePacket.HEADER_SIGN)));
    }
}
